package com.ultrasoft.farmasoft.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ultrasoft.farmasoft.domain.Locality;
import com.ultrasoft.farmasoft.domain.Userprovider;

@Repository(value = "userProviderDao")
@Transactional(readOnly = false)
public class UserProviderDaoImpl implements UserProviderDao {

	private EntityManager em = null;
	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;
	} 
	
	public List<Userprovider> getListUserProvider() {
		TypedQuery<Userprovider> q = em.createNamedQuery("Userprovider.findAll",Userprovider.class);
    	return q.getResultList();
	}

	public List<Userprovider> getListUserProvider(boolean active) {
		TypedQuery<Userprovider> q = em.createNamedQuery("Userprovider.findByActive",Userprovider.class);
    	q.setParameter("active",active);
    	return q.getResultList();
	}

	public List<Userprovider> getListUserProviderByRolProviderIdByActive(Integer rolProviderId,boolean active) {
		TypedQuery<Userprovider> q = em.createNamedQuery("Userprovider.findByRolProviderIdByActive",Userprovider.class);
    	q.setParameter("rolProviderId",rolProviderId);
    	q.setParameter("active",active);
    	return q.getResultList();
	}
	
	public List<Userprovider> getListUserProviderByRolProviderIdByProviderIdByActive(Integer rolProviderId,Integer providerId,boolean active) {
		TypedQuery<Userprovider> q = em.createNamedQuery("Userprovider.findByRolProviderIdByProviderIdByActive",Userprovider.class);
    	q.setParameter("rolProviderId",rolProviderId);
    	q.setParameter("providerId",providerId);
    	q.setParameter("active",active);
    	return q.getResultList();
	}
	
	public List<Userprovider> getListUserProviderByproviderId(Integer providerId) {
		TypedQuery<Userprovider> q = em.createNamedQuery("Userprovider.findByProviderId",Userprovider.class);
    	q.setParameter("providerId",providerId);
    	return q.getResultList();
	}

	public List<Userprovider> getListUserProviderByuserProviderId(Integer userProviderId) {
		TypedQuery<Userprovider> q = em.createNamedQuery("Userprovider.findByUserProviderId",Userprovider.class);
    	q.setParameter("userProviderId",userProviderId);
    	return q.getResultList();
	}

	public List<Userprovider> getListUserProviderByproviderId(Integer providerId, boolean active) {
		TypedQuery<Userprovider> q = em.createNamedQuery("Userprovider.findByProviderIdByActive",Userprovider.class);
    	q.setParameter("providerId",providerId);
    	q.setParameter("active",active);
    	return q.getResultList();
	}

	public List<Userprovider> getListUserProviderByuserProviderId(Integer userProviderId, boolean active) {
		TypedQuery<Userprovider> q = em.createNamedQuery("Userprovider.findByUserProviderIdByActive",Userprovider.class);
    	q.setParameter("userProviderId",userProviderId);
    	q.setParameter("active",active);
    	return q.getResultList();
	}

	public Userprovider findByUserProviderIdByProviderId(Integer userProviderId, Integer providerId) {
		Userprovider res = null;
    	Query  q = em.createNamedQuery("Userprovider.findByProviderIdByUserProviderId");
    	q.setParameter("userProviderId",userProviderId);
    	q.setParameter("providerId",providerId);
    	if(q.getResultList().size()==1)
    		res=(Userprovider)q.getSingleResult();
    	return res;
	}

	public void update(Userprovider userProvider) {
		em.merge(userProvider);
	}

	public void add(Userprovider userProvider) {
		em.persist(userProvider);
	}
	
	public void remove(Userprovider userProvider){
		Query q= em.createNamedQuery("Userprovider.deleteByProviderIdByUserProviderId");
		q.setParameter("userProviderId",userProvider.getUserproviderPK().getUserProviderId());
    	q.setParameter("providerId",userProvider.getUserproviderPK().getProviderId());
		q.executeUpdate();	
	}
	
	
	public Integer findByuserProviderIdMax(){
		Integer res=0;
		if(!this.getListUserProvider().isEmpty()){
			try {
				res=(Integer)em.createNamedQuery("Userprovider.findByUserProviderIdMax").getSingleResult();
			} catch (Exception e) {}
		}
		return res;
	}
}
